package com.autotest.nore.service;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.autotest.nore.domain.*;
import com.autotest.nore.mapper.*;
import com.autotest.nore.dto.*;
import com.autotest.nore.request.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
@Transactional(rollbackFor = Exception.class)
public class CaseService {

    @Autowired
    private CaseMapper caseMapper;

    @Autowired
    private CaseApiMapper caseApiMapper;

    public void updateCaseVariables(String caseVariables,String caseId){
        caseMapper.updateVariables(caseVariables,caseId);
    }

    public String saveCase2(CaseRequest caseRequest){
        JSONObject caseObject = (JSONObject) JSON.toJSON(caseRequest);
        Case testCase = caseObject.toJavaObject(Case.class);
        if(testCase.getId() == null || testCase.getId().equals("") ){
            // 新增用例
            String id=UUID.randomUUID().toString();
            testCase.setId(id);
            testCase.setCreateTime(System.currentTimeMillis());
            testCase.setUpdateTime(System.currentTimeMillis());
            testCase.setCreateUser(testCase.getUpdateUser());
            testCase.setStatus("Normal");
            caseMapper.addCase(testCase);
            return id;
        }else{ // 修改用例
            testCase.setUpdateTime(System.currentTimeMillis());
            caseMapper.updateCase(testCase);
            return testCase.getId();
        }
    }


    public void saveCase(CaseRequest caseRequest) {
        JSONObject caseObject = (JSONObject) JSON.toJSON(caseRequest);
        Case testCase = caseObject.toJavaObject(Case.class);
        if(testCase.getId().equals("") || testCase.getId() == null){ // 新增用例
            testCase.setId(UUID.randomUUID().toString());
            testCase.setCreateTime(System.currentTimeMillis());
            testCase.setUpdateTime(System.currentTimeMillis());
            testCase.setCreateUser(testCase.getUpdateUser());
            testCase.setStatus("Normal");
            caseMapper.addCase(testCase);
        }else{ // 修改用例
            testCase.setUpdateTime(System.currentTimeMillis());
            caseMapper.updateCase(testCase);
        }
        if(caseRequest.getType().equals("API")){    // 新增接口用例事务
            caseApiMapper.deleteCaseApi(testCase.getId());  //先删除全部用例接口
            List<CaseApiRequest> caseApiArray = caseRequest.getCaseApis();
            List<CaseApi> caseApis = new ArrayList<>();
            for(CaseApiRequest caseApiRequest: caseApiArray){
                JSONObject caseApiObject = (JSONObject) JSON.toJSON(caseApiRequest);
                CaseApi caseApi = caseApiObject.toJavaObject(CaseApi.class);
                caseApi.setCaseId(testCase.getId());
                caseApi.setId(UUID.randomUUID().toString());
                caseApis.add(caseApi);
            }
            caseApiMapper.addCaseApi(caseApis);
        }
    }

    public void deleteCase(CaseRequest caseRequest) {
        caseMapper.deleteCase(caseRequest.getId());
    }

    public String getCaseSystem(String caseId){
        return caseMapper.getCaseSystem(caseId);
    }

    public CaseDTO getCaseDetail2(String caseId) {
        // 1. 查询数据库（此时 caseVariablesString 有值，caseVariables 为 null）
        CaseDTO caseDTO = caseMapper.getCaseDetail(caseId);
        // 2. 手动转换 JSON 字符串
        if (caseDTO.getCaseVariablesString() != null) {
            JSONObject jsonObject = JSONObject.parseObject(caseDTO.getCaseVariablesString());
            caseDTO.setCaseVariables(jsonObject);
        }
        List<ApiCaseDTO> apiCases = caseApiMapper.getApiCaseListByCaseId(caseId);
        caseDTO.setApiCases(apiCases);
        return caseDTO;
    }

    public List<CaseDTO> getCaseList(QueryRequest request){
        if(request.getCondition() != null && !request.getCondition().equals("")){
            request.setCondition("%"+request.getCondition()+"%");
        }
        return caseMapper.getCaseList(request);
    }





}
